//
//  ZHDBHelper.m
//  VehicleInsuranceSupport
//
//  Created by TangBin on 2019/11/4.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "ZHDBHelper.h"







@implementation ZHDBHelper

+ (FMDatabaseQueue *)databaseQueue
{
    static FMDatabaseQueue *_databaseQueue;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _databaseQueue = [FMDatabaseQueue databaseQueueWithPath:dataBasePath];
    });
    return _databaseQueue;
}

+ (BOOL)isTableExist:(NSString *)tableName withDB:(FMDatabase *)db
{
    BOOL isExist = NO;
    
    FMResultSet *rs = [db executeQuery:@"select count(*) as 'count' from sqlite_master where type='table' and name=?;", tableName];
    while ([rs next])
    {
        NSInteger count = [rs intForColumn:@"count"];
        if (count == 0)
        {
            isExist = NO;
        }
        else
        {
            isExist = YES;
        }
    }
    [rs close];
    
    return isExist;
}

/// 创建表
/// @param sql sql
/// @param tableName 表名
+ (void)createTable:(NSString *)sql tableName:(NSString *)tableName
{
    FMDatabase *db = [FMDatabase databaseWithPath:dataBasePath];
    if ([db open])
    {
        NSString *strSql = [NSString stringWithFormat:sql, tableName];
        BOOL res = [db executeUpdate:strSql];
        if (res)
        {
            debugLog(@"db: success to create db table %@", strSql);
        }
        else
        {
            debugLog(@"db: failed to create db table %@", strSql);
        }
        [db close];
    }
    else
    {
        debugLog(@"db: error when open db.");
    }
}

/// 删除表
/// @param tableName 表名
+ (void)dropTableName:(NSString *)tableName
{
    FMDatabase *db = [FMDatabase databaseWithPath:dataBasePath];
    if ([db open])
    {
        NSString *strSql = [NSString stringWithFormat:@"DROP TABLE %@;", tableName];
        BOOL res = [db executeUpdate:strSql];
        if (res)
        {
            debugLog(@"db: success to drop table %@", strSql);
        }
        else
        {
            debugLog(@"db: failed to drop table %@", strSql);
        }
        [db close];
    }
    else
    {
        debugLog(@"db: error when open db.");
    }
}

/// 修改表结构
/// @param sql sql
/// @param tableName tableName description
+ (void)alterTableData:(NSString *)sql tableName:(NSString *)tableName
{
    FMDatabaseQueue *queue = [self databaseQueue];
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *strSql = [NSString stringWithFormat:sql, tableName];
        BOOL res = [db executeUpdate:strSql];
        if (res)
        {
            debugLog(@"db: success to alter table %@", strSql);
        }
        else
        {
            debugLog(@"db: failed to alter table %@", strSql);
        }
    }];
}

/// 删除表数据
/// @param sql sql
/// @param tableName 表名
+ (void)deleteTableData:(NSString *)sql tableName:(NSString *)tableName
{
    FMDatabaseQueue *queue = [self databaseQueue];
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *strSql = [NSString stringWithFormat:sql, tableName];
        BOOL res = [db executeUpdate:strSql];
        if (res)
        {
            debugLog(@"db: success to delete table %@", strSql);
        }
        else
        {
            debugLog(@"db: failed to delete table %@", strSql);
        }
    }];
}

/// 更新/添加数据数据
/// @param sql sql description
+ (void)updateExecuteSql:(NSString *)sql
{
    FMDatabaseQueue *queue = [self databaseQueue];
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
        BOOL result = [db executeUpdate:sql];
        if (result)
        {
            debugLog(@"db: update success %@", sql);
        }
        else
        {
            debugLog(@"db: update failed %@", sql);
        }
    }];
}

+ (void)querySql:(NSString *)sql resultSetBlock:(void(^)(FMResultSet *rs))resultSetBlock
{
    FMDatabaseQueue *queue = [self databaseQueue];
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
        FMResultSet *rs = [db executeQuery:sql];
        if (resultSetBlock)
        {
            resultSetBlock(rs);
        }
        if (rs)
        {
            [rs close];
        }
    }];
}

@end
